<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>

  <fmt:setBundle basename="ApplicationMessage" />
  <fmt:setLocale value="zh_CN" />

  <%@ include file="/WEB-INF/include/meta.jsp"%>
  <%@ include file="/WEB-INF/include/css.jsp"%>
  <link href="${ctx.resource}/css/select2.css" rel="stylesheet">
  <link href="${ctx.resource}/css/select2-bootstrap.css" rel="stylesheet">

</head>
<body>

  <%@ include file="/WEB-INF/include/header.jsp"%>
  
  <div class="web-g-main">

    <%@ include file="/WEB-INF/include/aside.jsp"%>
    <div class="content">
      <header class="header clearfix">
          <h3 class="pull-left">划入租约</h3>
       </header>
       <c:if test="${not empty infoMessage}">
         <div class="alert alert-success alert-dismissable alert-fade top-margin-10 text-align-center">
           <button type="button" class="close" data-dismiss="alert">&times;</button>
           <i class="glyphicon glyphicon-info-sign"></i>&nbsp;&nbsp;
           <fmt:message key="${infoMessage}" />
         </div>
       </c:if>      
       
       <div class="earth-toolbar-wrapper clearfix">
       	
       	<div>
		<div class="col-md-3">
		
			<label>通道名称</label>
			<label>${data.getParticalName(particalUniqueId) }</label>
			
		</div>
       	</div>
        
       </div>
          
       <div class="table-area">
           <table class="data-list">
            <thead>
              <tr>
                <th>序号</th>
                <th>租约编号</th>
                <th>承租人</th>
                <th>租期</th>
                <th>租金</th>
                <th>收租人</th>
                <th>付租方式</th>
                <th>状态</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <c:forEach var="noAssignContract" items="${noAssignContracts}" varStatus="status">
                <tr contractId = ${noAssignContract.id }>
                  <td><input type="checkbox" class ="contract-checkbox"></td>
                  <td>${noAssignContract.contractNo}</td>
                  <td>${noAssignContract.customer.name}</td>
                  <td>
                    <fmt:formatDate value="${noAssignContract.beginDate}" pattern="yyyy-MM-dd" /><br>
                                                            至<br>
                    <fmt:formatDate value="${noAssignContract.endDate}" pattern="yyyy-MM-dd" />
                  </td>
                  <td>${noAssignContract.monthFee}元/月</td>
                  <td>${noAssignContract.app.name}</td>
                  <td><fmt:message key="${noAssignContract.paymentInstrument.key}" /></td>
                  <td><fmt:message key="${noAssignContract.contractStatus.key}" /></td>
                  <td>
                    <a href="javascript:void(0)" class="hover-no-text-decoration assign-contract" title="划入通道">划入通道</a> &nbsp;&nbsp;
                  </td>
                </tr>
              </c:forEach>
            </tbody>
           </table>

          <div class="row">
				<div class="col-xs-2 col-xs-offset-8">
					<button class="btn btn-default contract-partical-select">全选/反选</button>
				</div>
				<div class="col-xs-2">
					<button class="btn btn-primary assign-all">全部划入</button>
				</div>
		  </div>
		  
          <c:choose>
            <c:when test="${not empty queryString}">
              <jsp:include page="/WEB-INF/include/page.jsp">
                <jsp:param name="url" value="/contract-partical/${particalUniqueId}/assign-contracts" />
                <jsp:param name="queryString" value="${queryString}" />
              </jsp:include>
            </c:when>
            <c:otherwise>
              <jsp:include page="/WEB-INF/include/page.jsp">
              <jsp:param value="/contract-partical/${particalUniqueId}/assign-contracts" name="url" />
              </jsp:include>
            </c:otherwise>
          </c:choose>

       </div>
       <%@ include file="/WEB-INF/include/footer.jsp"%>
       
    </div>

  </div>
  
  <div class="modal fade in bs-example-modal-sm" id="dialog" role="dialog" tabindex="-1" aria-labelledby="dialoglabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close close-dialog" aria-label="关闭" ><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="dialoglabel">提示</h4>
			</div>
			<div class="modal-body">
				<p></p>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default close-dialog">关闭</button>
			</div>
		</div>
	</div>
  </div>

  <%@ include file="/WEB-INF/include/script.jsp"%>
  <script src="${ctx.resource}/js/bootstrap-switch.min.js"></script>
  <script src="${ctx.resource}/js/bootstrap-modal-trigger.js"></script>
  <script src="${ctx.resource}/js/select2.min.js"></script>
  <script src="${ctx.resource}/js/select2_locale_zh-CN.js"></script>
  <script src="${ctx.resource}/js/bootstrap.validate.js"></script>
  <script src="${ctx.resource}/js/bootstrap.validate.en.js"></script>
  <script src="${ctx.resource}/js/earth-preupload.js"></script>
  <script src="${ctx.resource}/js/bootstrap-alert-fade-trigger.js"></script>
  <script>
  
	$(function(){
		
		var selectCount = 0;
		
		$(".contract-partical-select").click(function(){
			
			if(selectCount % 2 == 0){
				
				$(".contract-checkbox").attr("checked","checked");
			}else{
				
				$(".contract-checkbox").removeAttr("checked");
			}
			selectCount++;
			
			if(selectCount % 2 == 0){
				
				window.location.reload();
			}
		});
		
		$('.assign-all').click(function(){
			
			var $checkedBox  = $("input[type='checkbox']:checked");
			
			if($checkedBox.length <= 0){
				
				resultHandler("请选中要划入通道的租约！",false);
				
				return false;
				
			}
			
			var particalUniqueId = '${particalUniqueId}';
			
			var contractIds = [];
			
			$checkedBox.each(function(){
				
				var contractId = Number($(this).parents("tr").attr("contractId"));
				
				contractIds.push(contractId);
			});
			
			assignContracts(particalUniqueId,contractIds);
			
		});
		
		$(".assign-contract").click(function(){
			
			var particalUniqueId = '${particalUniqueId}';
			
			var contractIds = [];
			
			var contractId = Number($(this).parents("tr").attr("contractId"));
			
			contractIds.push(contractId);
			
			assignContracts(particalUniqueId,contractIds);
			
		});
		
		var resultHandler = function(message,isReload){
			 
			 $('#dialog').find('p').text(message).end().modal();
			 
			 $('#dialog .close-dialog').click(function(){
				 
				 $('#dialog').modal('hide');
				 
				 if(isReload){
				 
					 window.location.reload();
				};
			
			 });
		 };
		 
		 var assignContracts = function(particalUniqueId,contractIds){
			 
			$.post('${ctx}/contract-partical/do-assign-contracts',{
				
				uniqueId : particalUniqueId,
				
				contractIds : JSON.stringify(contractIds)
				
			}).done(function(data){
				
				var message = $.parseJSON(data).message;
				 
				 resultHandler(message,true);
			});
		 }
	});

</script>
  
</body>
</html>
